package com.dy.org.controller;

import com.dy.basic.util.AjaxResult;
import com.dy.basic.util.ExcelUtil;
import com.dy.basic.util.PageList;
import com.dy.org.domain.Shop;
import com.dy.org.dto.ShopAuditLogDto;
import com.dy.org.dto.ShopDto;
import com.dy.org.query.ShopQuery;
import com.dy.org.service.IShopService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){
        return shopService.findAll();
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {

        return shopService.queryByPage(query);
    }

    /**
     * 商家入驻
     *
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid ShopDto shopDto){
        shopService.settlement(shopDto);
        return AjaxResult.me().success();
    }

    /**
     * 商家入驻
     *
     */
    @PostMapping("/audit/pass")
    public AjaxResult audit(@RequestBody @Valid ShopAuditLogDto dto) throws Exception {
        shopService.audit(dto);
        return AjaxResult.me().success();
    }

    /**
     * 手动激活
     *
     */
    @GetMapping("/audit/jihuo/{id}")
    public AjaxResult jihuo(@PathVariable("id") Long id) {
        shopService.updateState(id);
        return AjaxResult.me().success();
    }

    /**
     * 导出
     *
     */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse resp) {
        List<Shop> all = shopService.findAll();
        ExcelUtil.exportExcel(all,null,"店铺信息",Shop.class,"店铺信息.xlsx",resp);
    }

    /**
     * 导入
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file")MultipartFile file) {
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        System.out.println(shops.size());
    }

    /**
     * 根据id查询用户的信息
     */
    @GetMapping("/findById/{id}")
    public Shop findById(@PathVariable("id") String id) {
        Long id2 = Long.valueOf(id);
        Shop shop = shopService.findById(id2);
        return shop;
    }

    /**
     * 根据id修改用户的信息
     */
    @PostMapping("/updateById")
    public AjaxResult updateById(@RequestBody Shop shop) {
        shop.setState(0);
        shopService.update(shop);
        return AjaxResult.me().success();
    }

    @GetMapping("/echarts")
    public Map<String, Object> loadShopEchartsData(){
        return shopService.loadShopEchartsData();
    }
}
